package high.concurrent.config;

import high.concurrent.enums.MQConstant;
import org.springframework.amqp.core.*;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.amqp.support.converter.MessageConverter;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * 关闭订单MQ
 */
@Configuration
public class CallBackOperateMQConfig {

    /**
     * 消息转换器，转成Json，这么写就行
     * @return
     */
    @Bean
    public MessageConverter messageConverter(){
        return new Jackson2JsonMessageConverter();
    }

    /**
     * 初始化【死信队列 与 延迟队列】交换机，通过创造模式
     * 选用topic类型交换机 ， 进行durable=true持久化
     * @return
     */
    @Bean(value = "order_exchange_job")
    public Exchange getExchangeJob(){
        return ExchangeBuilder.topicExchange(MQConstant.EXCHANGE_NAME) .durable(true).build();
    }

    /**
     * 初始化【死信队列】队列，通过创造模式
     * 进行durable配置队列名
     * @return
     */
    @Bean(value = "auth_queue_job")
    public Queue getQueueJob(){
        return QueueBuilder.durable(MQConstant.AUTH_QUEUE_NAME).build();
    }

    /**
     * 绑定死信【队列】与【交换机】
     * bing(队列).to(交换机).with(路由规则).noargs();
     * 路由规则注意*与#
     * @return
     */
    @Bean
    public Binding bingExchangeQueue(@Qualifier("auth_queue_job") Queue queue,
                                     @Qualifier("order_exchange_job") Exchange exchange){

        return BindingBuilder.bind(queue).to(exchange).with(MQConstant.AUTH_ROUTING_KEY).noargs();
    }


}
